package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/1/11 21:12
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private QiniuUtils qiniuUtils;
    @DubboReference
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        log.info("文件上传,name:{},size{}" + multipartFile.getOriginalFilename() + multipartFile.getSize());
        String originalFileName = multipartFile.getOriginalFilename();

        //使用UUID构造不重复的文件名  图片名称前面加上上传时的时间戳
        String fileName = System.currentTimeMillis() +"_"+ UUID.randomUUID().toString().replace("-", "") + "_" + originalFileName;

        //将文件标识写入Redis的set
        redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).add(fileName);
        //上传到七牛云
        try (InputStream is = multipartFile.getInputStream()) {
            qiniuUtils.upload2Qiniu(is, fileName);
        } catch (IOException e) {
            log.info("", e);
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);

        }
        //将文件标识写入Redis的set
        //redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).add(fileName);

        //构造返回值
        String pic = qiniuUtils.getUrlPrefix() + fileName;
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, pic);
    }

    /**
     * @Description:添加套餐
     * @Parameter:
     * @Return:
     **/
    @RequestMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        log.info("[套餐:添加]", checkgroupIds, setmeal);
        //因为图片上传是非必填选项,所以需要判断是否存在图片
        if (!StringUtils.isEmpty(setmeal.getImg())) {
            String img = setmeal.getImg();
            img = img.replace(qiniuUtils.getUrlPrefix(), "");
            setmeal.setImg(img);
            //从Redis的set中删除文件标识
            //redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(setmeal.getImg());
        }
        //调用service插入数据
        setmealService.add(checkgroupIds, setmeal);

        //从Redis的set中删除文件标识
        if (StringUtils.isEmpty(setmeal.getImg())) {
            redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(setmeal.getImg());
        }
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);

    }

    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[套餐:分页查询]", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, pageResult);
    }
}
